package com.yedou.service.impl;

import com.yedou.convert.SubjectInfoConverter;
import com.yedou.entity.PageResponse;
import com.yedou.entity.po.SubjectInfo;
import com.yedou.entity.SubjectInfoReq;
import com.yedou.mapper.SubjectInfoDao;
import com.yedou.service.SubjectInfoService;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 题目信息表(SubjectInfo)表服务实现类
 *
 * @author makejava
 * @since 2025-09-06 11:44:24
 */
@Service("subjectInfoService")
public class SubjectInfoServiceImpl implements SubjectInfoService{
    @Resource
    private SubjectInfoDao subjectInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SubjectInfo queryById(Long id) {
        return this.subjectInfoDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param

     * @return 查询结果
     */
    @Override
    public PageResponse<SubjectInfo> queryByPage(SubjectInfoReq subjectInfoReq) {
        SubjectInfo subjectInfo = SubjectInfoConverter.INSTANCE.convertReqToSubjectInfo(subjectInfoReq);
        PageResponse<SubjectInfo> pageResponse = new PageResponse<>();
        Long pageNo = subjectInfoReq.getPageNo();
        Long pageSize = subjectInfoReq.getPageSize();
        pageResponse.setCurrent(pageNo);
        pageResponse.setPageSize(pageSize);
        Long pageStart = (pageNo - 1) * pageSize;
        Long total = this.subjectInfoDao.count(subjectInfo);
        List<SubjectInfo> subjectInfoList = this.subjectInfoDao.queryAllByLimit(subjectInfo, pageStart, pageSize);
        pageResponse.setTotal(total);
        pageResponse.setRecords(subjectInfoList);
        return pageResponse;
    }

    /**
     * 新增数据
     *
     * @param subjectInfo 实例对象
     * @return 实例对象
     */
    @Override
    public SubjectInfo insert(SubjectInfo subjectInfo) {
        // 公共字段
        subjectInfo.setCreatedBy("admin");
        subjectInfo.setCreatedTime(new Date());
        subjectInfo.setUpdateBy("admin");
        subjectInfo.setUpdateTime(new Date());
        subjectInfo.setIsDeleted(0);
        this.subjectInfoDao.insert(subjectInfo);
        return subjectInfo;
    }

    /**
     * 修改数据
     *
     * @param subjectInfo 实例对象
     * @return 实例对象
     */
    @Override
    public SubjectInfo update(SubjectInfo subjectInfo) {
        this.subjectInfoDao.update(subjectInfo);
        return this.queryById(subjectInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.subjectInfoDao.deleteById(id) > 0;
    }

    @Override
    public int countByCondition(SubjectInfo subjectInfo, Long categoryId, Long labelId) {
        return this.subjectInfoDao.countByCondition(subjectInfo, categoryId, labelId);
    }

    @Override
    public List<SubjectInfo> queryPage(SubjectInfo subjectInfo, Long categoryId, Long labelId, int start, Integer pageSize) {
        return this.subjectInfoDao.queryPage(subjectInfo, categoryId, labelId, start, pageSize);
    }
}
